import 'dart:io';

import 'package:auto_orientation/auto_orientation.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/dao/my_dao.dart';
import 'package:hktk_app/dao/package_dao.dart';
import 'package:hktk_app/dialog/buy_dialog.dart';
// import 'package:hktk_app/dao/package_dao.dart';
import 'package:hktk_app/exception/dao_exception.dart';
import 'package:hktk_app/model/data_model.dart';
import 'package:hktk_app/model/package/column_package_model.dart';
import 'package:hktk_app/model/package/package_model.dart';
import 'package:hktk_app/util/font_util.dart';
// import 'package:hktk_app/util/screen_adapter_helper.dart';
import 'package:hktk_app/util/toast_util.dart';
import 'package:hktk_app/widget/package/purchase_widget.dart';
// import 'package:hktk_app/widget/package/purchase_widget.dart';

/// 这里是显示常用的小组件

/// 设置默认的间距组件
SizedBox hiSpace({double height = 1, double width = 1}) {
  return SizedBox(
    height: height,
    width: width,
  );
}

/// 推荐区内容分割间距
SizedBox recommendItemSeparator() {
  return hiSpace(height: 24);
}

/// 根据条件显示
showListViewOrColum(bool hei, List<Widget> list) {
  if (hei) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: list,
    );
  } else {
    return ListView(
      children: list,
    );
  }
}

/// 根据难度显示相关的星星数量
showDifficulty({required int num}) {
  if (num > 5) {
    num = 5;
  } else if (num < 1) {
    num = 1;
  }
  List<Widget> list = [];
  for (int i = 0; i < num; i++) {
    list.add(Container(
      width: 28,
      height: 28,
      padding: EdgeInsets.only(left: 3, top: 1, right: 3, bottom: 3),
      child: SvgPicture.asset(
        "images/subject/star.svg",
        width: 24,
        height: 22,
      ),
    ));
    if (i != num - 1) {
      list.add(hiSpace(width: 6));
    }
  }
  return Row(
    mainAxisSize: MainAxisSize.min,
    children: [
      Text(
        "难度",
        style: FontUtil.fontGrey2(24),
      ),
      hiSpace(width: 10),
      ...list,
    ],
  );
}

/// 获取对应的时间方法
String getDateStr(int num) {
  String str = num.toString();
  if (str.length < 2) {
    str = "0$str";
  }
  return str;
}

/// 显示下拉结束
Widget showListEnd() {
  return Container();
  // return SizedBox(
  //   height: 100,
  //   child: Center(
  //     child: Text(
  //       "我是有底线的！",
  //       style: FontUtil.fontBlue1(22),
  //     ),
  //   ),
  // );
}

/// 显示暂无数据
Widget showNoData({
  String? text,
  TextStyle? style,
}) {
  return Center(
    child: Text(
      text ?? "暂无数据",
      style: style ?? FontUtil.fontGrey1(18),
    ),
  );
}

/// 下载按钮
Widget downloadBtn() {
  return Padding(padding: EdgeInsets.only(right: 10),child: Text(
    "下载",
    style: FontUtil.fontBlue1(20),
  ));
}

/// 切换屏幕方法
/// isHorizontal：是否横屏，true：横屏，false；竖屏
void switchScreen(bool isHorizontal) {
  if (isHorizontal) {
    // 设置竖屏
    _setVertical();
  } else {
    // 设置横屏
    _setHorizontal();
  }
}

/// 设置横屏方法
_setHorizontal() {
  AutoOrientation.landscapeAutoMode();
  if (Platform.isAndroid) {
    ///关闭状态栏，与底部虚拟操作按钮
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
  }
}

/// 设置竖屏方法
_setVertical() {
  AutoOrientation.portraitAutoMode();
  if (Platform.isAndroid) {
    ///显示状态栏，与底部虚拟操作按钮
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
        overlays: [SystemUiOverlay.top, SystemUiOverlay.bottom]);
  }
}

/// 获取相关数据方法
void packageFun({
  required BuildContext context,
  required String columnId,
  required DataModel dataModel,
}) {
  // 开启加载框
  ToastUtil.loadingBoxShow(context);
  // 加载数据
  _getPackage(
    columnId: columnId,
    dataModel: dataModel,
  ).then((Map<String, dynamic> map) {
    // 关闭加载框
    ToastUtil.loadingBoxRemove();
    if (map["suc"]) {
      // 加载成功，弹出购买弹窗
      _onTipsSetState(
        context: context,
        packageModel: map["package"],
        payHide: map["payHide"],
      );
    } else {
      // 加载失败
      ToastUtil.show(context: context, msg: map["msg"]);
    }
  });
}

/// 请求方法
Future<Map<String, dynamic>> _getPackage({
  required String columnId,
  required DataModel dataModel,
}) async {
  Map<String, dynamic> map = {};
  try {
    // 获取套餐相关信息
    ColumnPackageModel model = await PackageDao.getPackage(
      columnId: columnId,
      examId: dataModel.examId,
      categoryId: dataModel.categoryId,
    );
    // ColumnPackageModel model = new ColumnPackageModel();
    if (model.package != null) {
      model.package!.skuModels?.forEach((SkuModel sku) {
        bool sss = listEquals(model.productIds, sku.productIds);
       sku.isSelect = sss;
      });

      // 获取是否需要隐藏支付方式
      bool payHide = await MyDao.versionCurrentIos();
      map["package"] = model.package;
      map["payHide"] = payHide;
      map["suc"] = true;
    } else {
      map["suc"] = false;
      map["msg"] = "未知错误，请重试";
    }
  } catch (e) {
    map = {"suc": false, "msg": "未知错误，请重试"};
    if (e is DaoException) {
      map["msg"] = e.msg;
    }
  }
  return map;
}

/// 弹出购买框方法
void _onTipsSetState({
  required BuildContext context,
  required PackageModel packageModel,
  required bool payHide,
}) {
  // 获取弹出层高度
  var height = ScreenUtil().screenHeight;
  double sheetHeight = height * 0.8;

  // () async {
  //   bool flag=false;
  //   if(Platform.isIOS){
  //     var res=await MyDao.versionCurrentIos();
  //     flag=res ;
  //   }

    showModalBottomSheet(
      context: context,
      // 设置底部弹出框全屏显示
      isScrollControlled: true,
      // 自定义弹出层高度
      constraints: BoxConstraints(maxHeight: sheetHeight),
      // 自定义圆角
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(30),
          topRight: Radius.circular(30),
        ),
      ),
      builder: (BuildContext context) {
        return BuyDialog(
          package: packageModel,
          index: 0,
          // payHide: Platform.isIOS?!payHide:false,
          payHide: payHide,
        );
        // return PurchaseWidget(
        //   package: packageModel,
        //   index: 0,
        //   payHide: payHide,
        // );
      },
    );
}



/// 浅灰色
const Color colorGrey1 = Color.fromRGBO(153, 154, 156, 1);

/// 深灰色
const Color colorGrey2 = Color.fromRGBO(108, 110, 112, 1);

/// 灰色
const Color colorGrey3 = Color.fromRGBO(205, 214, 231, 1);

/// 灰色4 购买页面卡片白色内线条
const Color colorGrey4 = Color.fromRGBO(234, 234, 235, 1);

/// 灰色5 购买页面搜索框对应字体颜色
const Color colorGrey5 = Color.fromRGBO(177, 181, 184, 1);

/// 灰色6，选择科目页面添加科目禁止标签字体颜色
const Color colorGrey6 = Color.fromRGBO(188, 189, 189, 1);

/// 灰色7，购买页面分数中间分割
const Color colorGrey7 = Color.fromRGBO(217, 217, 217, 1);

/// 灰色8，试卷详情分割线
const Color colorGrey8 = Color.fromRGBO(177, 181, 184, 0.2);

/// 灰色9，我的页面头部提示
const Color colorGrey9 = Color.fromRGBO(154, 155, 156, 1);

/// 灰色10，答题卡未答颜色
const Color colorGrey10 = Color.fromRGBO(238, 238, 238, 1);

/// 灰色11，答题卡字体颜色
const Color colorGrey11 = Color.fromRGBO(134, 143, 158, 1);

/// 灰色12，答题结果 考试情况背景
const Color colorGrey12 = Color.fromRGBO(246, 247, 251, 1);

/// 灰色13，答题结果 考试情况分割线
const Color colorGrey13 = Color.fromRGBO(230, 233, 238, 1);

/// 灰色14，激活题库页面虚线
const Color colorGrey14 = Color.fromRGBO(232, 233, 233, 1);

/// 灰色15，设置页面提示字体
const Color colorGrey15 = Color.fromRGBO(199, 199, 200, 1);

/// 灰色16，资料下载页面
const Color colorGrey16 = Color.fromRGBO(235, 235, 235, 1);

/// 灰色17，订单支付选择支付方式边框
const Color colorGrey17 = Color.fromRGBO(237, 237, 237, 1);

/// 灰色18，资料预览页面遮罩背景中间色
const Color colorGrey18 = Color.fromRGBO(135, 135, 135, 0.26);

/// 灰色19，做题页面笔记头像边框
const Color colorGrey19 = Color.fromRGBO(226, 226, 228, 1);

/// 黑色
const Color colorBlack1 = Color.fromRGBO(30, 32, 35, 1);

/// 黑色2 购买页面白色阴影
const Color colorBlack2 = Color.fromRGBO(0, 0, 0, 0.1);

/// 黑色3 购买页面卡片顶部阴影
const Color colorBlack3 = Color.fromRGBO(0, 0, 0, 0.9);

/// 黑色4，购买页面弹出层底部阴影
const Color colorBlack4 = Color.fromRGBO(0, 0, 0, 0.08);

/// 黑色5，获取权限页面不同意按钮边框颜色
const Color colorBlack5 = Color.fromRGBO(30, 32, 35, 0.1);

/// 黑色6，章节练习、历年真题等页面未购买提示背景
const Color colorBlack6 = Color.fromRGBO(30, 32, 35, 0.64);

/// 黑色7，做题页面草稿纸背景
const Color colorBlack7 = Color.fromRGBO(0, 0, 0, 0.5);

/// 黑色8，答题结果页面底部阴影
const Color colorBlack8 = Color.fromRGBO(0, 0, 0, 0.06);

/// 黑色9，答题结果页考试情况
const Color colorBlack9 = Color.fromRGBO(0, 0, 0, 1);

/// 黑色10，意见反馈页面问题类型字体颜色
const Color colorBlack10 = Color.fromRGBO(38, 38, 38, 1);

/// 黑色11，资料预览页面遮罩背景结束色
const Color colorBlack11 = Color.fromRGBO(0, 0, 0, 0.56);

/// 黑色12，随机小测做题页面暗黑模式背景
const Color colorBlack12 = Color.fromRGBO(18, 19, 37, 1);

/// 黑色13，随机小测导航栏背景
const Color colorBlack13 = Color.fromRGBO(30, 31, 49, 1);

/// 黑色14，随机小测工具箱暗黑模式背景
const Color colorBlack14 = Color.fromRGBO(52, 53, 70, 1);

/// 黑色15，加载框遮罩层
const Color colorBlack15 = Color.fromRGBO(30, 32, 35, 0.4);

/// 蓝色
const Color colorBlue1 = Color.fromRGBO(10, 113, 255, 1);

/// 淡灰蓝色
const Color colorBlue2 = Color.fromRGBO(227, 239, 255, 1);

/// 蓝色3 购买页面卡片底层背景起始
const Color colorBlue3 = Color.fromRGBO(3, 195, 255, 1);

/// 蓝色4 购买页面卡片底层背景结束
const Color colorBlue4 = Color.fromRGBO(138, 227, 255, 1);

/// 蓝色5 购买页面卡片顶层边框起始
const Color colorBlue5 = Color.fromRGBO(169, 219, 255, 1);

/// 蓝色6 购买页面卡片顶层背景起始
const Color colorBlue6 = Color.fromRGBO(0, 194, 255, 1);

/// 蓝色7 购买页面卡片顶层背景结束
const Color colorBlue7 = Color.fromRGBO(93, 216, 255, 1);

/// 蓝色8，购买页面深蓝色卡片底层背景起始
const Color colorBlue8 = Color.fromRGBO(31, 108, 255, 1);

/// 蓝色9，购买页面深蓝色卡片底层背景结束
const Color colorBlue9 = Color.fromRGBO(127, 198, 255, 1);

/// 蓝色10，购买页面深蓝色卡片顶层边框起始
const Color colorBlue10 = Color.fromRGBO(153, 210, 252, 1);

/// 蓝色11，购买页面深蓝色卡片顶层背景起始
const Color colorBlue11 = Color.fromRGBO(30, 106, 255, 1);

/// 蓝色12，购买页面深蓝色卡片顶层背景结束
const Color colorBlue12 = Color.fromRGBO(104, 188, 254, 1);

/// 蓝色13，购买页面卡片顶层边框结束
const Color colorBlue13 = Color.fromRGBO(169, 219, 255, 0);

/// 蓝色14，购买页面深蓝色卡片顶层边框结束
const Color colorBlue14 = Color.fromRGBO(153, 210, 252, 0);

/// 蓝色15，购买页面卡片 tab(小标题)边框颜色
const Color colorBlue15 = Color.fromRGBO(200, 245, 255, 1);

/// 蓝色16，购买页面深蓝色卡片 tab(小标题)边框颜色
const Color colorBlue16 = Color.fromRGBO(200, 229, 255, 1);

/// 蓝色17，购买页面卡片按钮颜色
const Color colorBlue17 = Color.fromRGBO(2, 188, 246, 1);

/// 蓝色18，购买页面卡片白色内边框
const Color colorBlue18 = Color.fromRGBO(222, 239, 244, 1);

/// 蓝色19，购买页面深蓝色卡片白色内边框
const Color colorBlue19 = Color.fromRGBO(222, 229, 244, 1);

/// 蓝色20，章节练习、历年真题未购买提示按钮起始色
const Color colorBlue20 = Color.fromRGBO(76, 150, 255, 1);

/// 蓝色21，章节练习、历年真题未购买提示按钮结束色
const Color colorBlue21 = Color.fromRGBO(42, 103, 255, 1);

/// 蓝色22，我的页面背景色开始
const Color colorBlue22 = Color.fromRGBO(206, 227, 255, 1);

/// 蓝色23，我的页面背景色结束
const Color colorBlue23 = Color.fromRGBO(241, 245, 248, 0);

/// 蓝色24，我的页面/我的错题卡片边框、背景色起始
const Color colorBlue24 = Color.fromRGBO(245, 246, 255, 1);

/// 蓝色25, 答题结果正确率渐变背景
const Color colorBlue25 = Color.fromRGBO(211, 229, 254, 1.0);

/// 蓝色26, 做题页面暗黑模式对应蓝色
const Color colorBlue26 = Color.fromRGBO(59, 140, 254, 1.0);

/// 浅粉色
const Color colorPink1 = Color.fromRGBO(255, 231, 231, 1);

/// 深红色
const Color colorRed1 = Color.fromRGBO(93, 7, 5, 1);

/// 红色
const Color colorRed2 = Color.fromRGBO(253, 70, 62, 1);

/// 红色3，我的页面/我的错题卡片边框色起始
const Color colorRed3 = Color.fromRGBO(254, 244, 244, 1);

/// 红色4，我的页面/我的错题卡片背景色起始
const Color colorRed4 = Color.fromRGBO(254, 243, 243, 1);

/// 红色5，答题卡错误颜色
const Color colorRed5 = Color.fromRGBO(254, 74, 70, 1);

/// 红色6，做题页面草稿纸
const Color colorRed6 = Color.fromRGBO(255, 0, 0, 1);

/// 红色7，资料下载页面剩余字体颜色
const Color colorRed7 = Color.fromRGBO(255, 77, 77, 1);

/// 红色8，做题页面草稿纸暗黑模式
const Color colorRed8 = Color.fromRGBO(252, 109, 107, 1.0);

/// 橙色1，购买页面卡片底层背景起始
const Color colorOrange1 = Color.fromRGBO(255, 109, 44, 1);

/// 橙色2，购买页面卡片底层背景结束
const Color colorOrange2 = Color.fromRGBO(255, 175, 108, 1);

/// 橙色3，购买页面卡片顶层边框起始
const Color colorOrange3 = Color.fromRGBO(255, 225, 169, 1);

/// 橙色4，购买页面卡片顶层背景起始
const Color colorOrange4 = Color.fromRGBO(255, 98, 30, 1);

/// 橙色5，购买页面卡片顶层背景结束
const Color colorOrange5 = Color.fromRGBO(255, 170, 93, 1);

/// 橙色6，购买页面卡片价格字体颜色
const Color colorOrange6 = Color.fromRGBO(255, 106, 36, 1);

/// 橙色7，购买页面卡片顶层边框结束
const Color colorOrange7 = Color.fromRGBO(255, 225, 169, 0);

/// 橙色8，购买页面卡片tab（小标题）边框颜色
const Color colorOrange8 = Color.fromRGBO(255, 235, 200, 1);

/// 橙色9，购买页面卡片按钮颜色
const Color colorOrange9 = Color.fromRGBO(248, 92, 21, 1);

/// 橙色10，购买页面卡片白色内边框
const Color colorOrange10 = Color.fromRGBO(244, 231, 222, 1);

/// 橙色11，试卷详情
const Color colorOrange11 = Color.fromRGBO(240, 224, 212, 1);

/// 橙色12，我的错题提示字体颜色
const Color colorOrange12 = Color.fromRGBO(255, 122, 0, 1);

/// 橙色13，首页试卷标签内文字
const Color colorOrange13 = Color.fromRGBO(229, 140, 50, 1);

/// 橙色14，随机组卷考试背景
const Color colorOrange14 = Color.fromRGBO(254, 168, 107, 1);

/// 橙色15，随机组卷考试背景2
const Color colorOrange15 = Color.fromRGBO(235, 131, 70, 1);

/// 橙色16，随机组件横杠
const Color colorOrange16 = Color.fromRGBO(255, 216, 189, 1);

/// 黄色1
const Color colorYellow1 = Color.fromRGBO(255, 187, 0, 1);

/// 黄色2，我的页面/我的错题卡片边框色起始
const Color colorYellow2 = Color.fromRGBO(253, 250, 239, 1);

/// 黄色3，我的页面/我的错题卡片背景色起始
const Color colorYellow3 = Color.fromRGBO(253, 249, 237, 1);

/// 黄色4，小测日记提示
const Color colorYellow4 = Color.fromRGBO(255, 187, 0, 0.1);

/// 黄色5，我的错题提示
const Color colorYellow5 = Color.fromRGBO(255, 245, 229, 1);

/// 棕色
const Color colorBrown1 = Color.fromRGBO(94, 55, 11, 1);

/// 白色1
const Color colorWhite1 = Color.fromRGBO(255, 255, 255, 1);

/// 白色2，首页背景色
const Color colorWhite2 = Color.fromRGBO(242, 243, 247, 1);

/// 白色3，试卷详情背景色
const Color colorWhite3 = Color.fromRGBO(254, 251, 240, 1);

/// 白色4，意见反馈问题类型选中背景色
const Color colorWhite4 = Color.fromRGBO(255, 255, 255, 0.9);

/// 白色5，资料预览遮罩起始色
const Color colorWhite5 = Color.fromRGBO(255, 255, 255, 0);

/// 白色6，深色遮罩颜色
const Color colorWhite6 = Color.fromRGBO(255, 255, 255, 0.2);

/// 白色7，首页背景提示
const Color colorWhite7 = Color.fromRGBO(255, 255, 255, 0.4);

/// 白色8，首页底部导航栏
const Color colorWhite8 = Color.fromRGBO(255, 255, 255, 0.8);

/// 绿色1，购买页面卡片底层背景起始
const Color colorGreen1 = Color.fromRGBO(1, 191, 119, 1);

/// 绿色2，购买页面卡片底层背景结束
const Color colorGreen2 = Color.fromRGBO(20, 250, 168, 1);

/// 绿色3，购买页面卡片顶层边框起始
const Color colorGreen3 = Color.fromRGBO(169, 255, 209, 1);

/// 绿色4，购买页面卡片顶层背景起始
const Color colorGreen4 = Color.fromRGBO(0, 188, 117, 1);

/// 绿色5，购买页面卡片顶层背景结束
const Color colorGreen5 = Color.fromRGBO(11, 220, 141, 1);

/// 绿色6，购买页面卡片顶层边框结束
const Color colorGreen6 = Color.fromRGBO(169, 255, 209, 0);

/// 绿色7，购买页面卡片tab（小标题）边框颜色
const Color colorGreen7 = Color.fromRGBO(200, 255, 235, 1);

/// 绿色8，购买页面卡片按钮颜色
const Color colorGreen8 = Color.fromRGBO(1, 179, 111, 1);

/// 绿色9，购买页面卡片白色内边框
const Color colorGreen9 = Color.fromRGBO(222, 244, 229, 1);

/// 绿色10，答题卡答对颜色
const Color colorGreen10 = Color.fromRGBO(11, 172, 49, 1);

/// 绿色11，答题卡答对颜色暗黑模式
const Color colorGreen11 = Color.fromRGBO(60, 188, 90, 1.0);

/// 根据夜间模式获取对应的颜色，灰色10
Color getColorGrey10({required bool szYjms}) {
  return szYjms ? colorBlack14 : colorGrey10;
}

/// 根据夜间模式获取对应的颜色，灰色11
Color getColorGrey11({required bool szYjms}) {
  return szYjms ? colorGrey1 : colorGrey11;
}

/// 根据夜间模式获取对应的颜色，白色1
Color getColorBlack1({required bool szYjms}) {
  return szYjms ? colorWhite1 : colorBlack1;
}

/// 根据夜间模式获取对应的颜色，白色1
Color getColorWhite1({required bool szYjms}) {
  return szYjms ? colorBlack13 : colorWhite1;
}

/// 根据夜间模式获取对应的颜色，白色1
Color getColorWhite1b12({required bool szYjms}) {
  return szYjms ? colorBlack12 : colorWhite1;
}

/// 根据夜间模式获取对应的颜色，蓝色1
Color getColorBlue1({required bool szYjms}) {
  return szYjms ? colorBlue26 : colorBlue1;
}

/// 根据夜间模式获取对应的颜色，蓝色2
Color getColorBlue2({required bool szYjms}) {
  return szYjms ? colorBlack14 : colorBlue2;
}

/// 根据夜间模式获取对应的颜色，红色5
Color getColorRed5({required bool szYjms}) {
  return szYjms ? colorRed8 : colorRed5;
}

/// 根据夜间模式获取对应的颜色，绿色10
Color getColorGreen10({required bool szYjms}) {
  return szYjms ? colorGreen11 : colorGreen10;
}

/// 根据夜间模式获取对应的颜色，橙色12
Color getColorOrange12({required bool szYjms}) {
  return szYjms ? colorOrange13 : colorOrange12;
}
